<?
ob_start();
include("common/header.html");
include("common/left.html");
$out_html=ob_get_contents();
ob_end_clean();
include("common/page.php");
include("class/OrderRecord.php");
include("class/ProOrder.php");
include("class/SimpleExcel.class.php");
$atype= 'sale';// 导出文件名判断条件
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
//如果是admin，就不要下拉
// 获取分销商
include 'getDistors.php';
if($_SESSION['dtype']==7){// 集团账号获取供应商和分销商
	$disList = getAll($GLOBALS['le'],$_SESSION['sid']);
  
}elseif($_SESSION['dtype']!=7 && $_SESSION['dtype']!=9){
	$disList = getRetail($GLOBALS['le']);
}
// admin 账号
if($_SESSION['memberID']==1){
	// $disList = getAllMember($GLOBALS['le']);
	$disList = array();
}
$dTypeName = '分销商';

if($_GET['Z']){
print_r($disList);exit;
}


//print_r($anames);
$pOrder=saleProOrderObject($memberID,$s,$_SESSION['sdtype']);
//$v['title']="已卖出的产品";
$v['title'] ="已卖出的产品";
$v['title2']="分销订单";
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:15;
// 导出订单设置
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	$currentPage=1;
	$pageSize=1000;
	set_time_limit(1800);
}

$r=$pOrder->_orderlist($currentPage,$pageSize);




$fids=array();
$orderCount = $ticketCount = $allMoney = 0;// 订单统计信息
$showTel = array(
    '123513','123721','123777','100016','123818','502601','123824','123794','100001','123990','100096','100009','123707','123950','123951','123952','123953','123954','123955'
);
//悠游吧的员工帐号
$youyouba = array (
  0 => 'yytx009',
  1 => 'yytx010',
  2 => 'yyb0006',
  3 => 'yytx011',
  4 => 'yytx888',
  5 => 'zhangjiayi',
  6 => 'daizihan',
  7 => 'luyuan',
  8 => 'dengfengying',
  9 => 'xiaoxi',
  10 => 'liangjinglin',
  11 => 'lichangwei',
  12 => 'zhoumengke',
  13 => 'teshudingdan',
);
$showTel = array_merge($showTel,$youyouba);

$mids = array();
 if(count($r)){
        foreach($r as $key=>$row){
            // 直接供应商查看价格
            if($_SESSION['dtype']==2){
                $r[$key]['mainOrder']->UUtprice = $s->get_Dynamic_Price_Merge((string)$row['mainOrder']->UUpid,(string)$row['mainOrder']->UUbegintime,1);
            }
            
            
            if($r[$key]['mainOrder']->UUlid!=72 && !in_array($_SESSION['account'],$showTel)){
                if($r[$key]['mainOrder']->UUstatus!=1){
                    $r[$key]['mainOrder']->UUordertel=substr_replace((string)$r[$key]['mainOrder']->UUordertel,"****",3,4);
                }
            }
            
            $fids[]=(string)$row['mainOrder']->UUbuyid;
            if($_SESSION['dtype']==2) $aid = (string)$row['mainOrder']->UUsellid;
            
            $r[$key]['totalPrice'] = ((string)$row['mainOrder']->UUtprice*(string)$row['mainOrder']->UUtnum);
            // 统计订单信息
            $orderCount++;
            $ticketCount+=(string)$row['mainOrder']->UUtnum;
            $allMoney+=((int)$row['mainOrder']->UUtprice*(int)$row['mainOrder']->UUtnum)/100;
            
            if(!isset($mids[(string)$row['mainOrder']->UUbuyid])){
                //如果是套票的话 要重新取一次供应商
                if((string)$row['mainOrder']->UUconcat_id!=0){
                       $c_aid = $_SESSION['sdtype']==9 ? '' : $_SESSION['sid'];
                       $oid = (string)$row['mainOrder']->UUordernum;
                       $x= simplexml_load_string($s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '', '', '', $oid, '', '', '', '', '', '1', '1', '0', '10', '0', '0', '', '0', '', '0', '', '', $c_aid, '0', '0', '0'));
                       $mids[(string)$x->Rec->UUbuyid] = (string)$x->Rec->UUbuyid;
                       // echo (string)$row['mainOrder']->UUordernum;
                       // print_r($x);
                    }else{
                        $mids[(string)$row['mainOrder']->UUbuyid]=(string)$row['mainOrder']->UUbuyid;
                    }
            }
            
        }
    // print_r($mids);
    $str="SELECT id,dname FROM pft_member where id IN(".implode(',',$mids).")";
    $anames=array();
    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $anames[$row['id']]=$row['dname'];
    }
}
// print_r($anames);
$tmp = $r;
$arr = array();
foreach($r as $key => $row){
	// 处理
	$concat = (string)$row['mainOrder']->UUconcat_id;
	$orderid = (string)$row['mainOrder']->UUordernum;
	if($concat==0){
		$arr[] = $r[$key];// 主订单
		continue;
	}
	if($concat!=$orderid){
		$tmp[$key]= $r[$key];
		continue;
	}
	// 主订单并且含有从订单
	foreach($tmp as $k=>$data){
		if((string)$data['mainOrder']->UUconcat_id==$orderid && (string)$data['mainOrder']->UUordernum!=$orderid){
			$r[$key]['childOrder'][] = $data;
			unset($tmp[$k]);
		}
	}
	$arr[] = $r[$key];
}
$r = $arr;
if($fids && ($_SESSION['dtype']!=2)){
    
	$sel="select id,dname from pft_member where id in (".implode(",",$fids).")";
	$fnames=array();
	$GLOBALS['le']->query($sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$fnames[$row['id']]=$row['dname'];
	}
}

include("loadExcel_simple.php");
echo $out_html;

// 判断订单是否为主订单  true为主订单
function isMainOrder($field){
	return ($field=='main' || $field!=0 ) ? true:false;
}
// 复票订单主订单
function echoMainOrder($obj){
	return ((String)$obj->UUconcat_id=='main') ? (String)$obj->UUordernum:(String)$obj->UUconcat_id;
}
$count=$pOrder->orderlistTotal();
$arr_salerid=array(501610,501612);
$uuStatus = array(
	0=>"未使用",
	1=>"<em class='green'>已使用</em>",
	2=>"已过期",
	3=>"<em class='red'>已取消</em>",
	4=>"被替代",
	5=>"被修改",
	6=>"已撤销",
	7=>"部分使用"
);

$transFlag = true;
// 不显示分销商列
$showDtrArr = array(2223);
$showDtr = true;
if(in_array($_SESSION['sid'], $showDtrArr)){
	$showDtr = false;
}
if($_GET['a'])
    print_r($r);

$v['from'] = 'sale';
include("tpl/orderlist_test.html");
include("common/footer.html");
?>